package com.neusoft.elm.infrastructure.exception;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class GlobalExceptionHandler {
    
    public static void handleException(Exception e, HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        
        String errorMessage = "系统错误";
        if (e instanceof BusinessException) {
            errorMessage = e.getMessage();
        }
        
        out.print("{\"code\":500,\"message\":\"" + errorMessage + "\"}");
        out.close();
    }
} 